package cn.com.chengmandian.bike.service.impl;

import cn.com.chengmandian.bike.model.EbPhotosStop;
import cn.com.chengmandian.bike.mapper.EbPhotosStopMapper;
import cn.com.chengmandian.bike.service.IEbPhotosStopService;
import cn.com.chengmandian.core.web.exceptions.BadResponseException;
import cn.com.chengmandian.service.bike.constants.PhotosStopCheckResultEnum;
import cn.com.chengmandian.service.bike.constants.PhotosStopResponseCodeEnum;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.io.File;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 电单车运营-AI停车拍照记录 服务实现类
 * </p>
 *
 * @author AutoGenerator
 * @since 2024-10-15
 */
@Slf4j
@Service
public class EbPhotosStopServiceImpl extends ServiceImpl<EbPhotosStopMapper, EbPhotosStop> implements IEbPhotosStopService {

    // 华惠API车牌号识别接口
    private static final String HUA_HUI_PORT_URL = "https://aivi.huahuizhineng.com:660/recognition/parkingByPlateCode";

    @Override
    public void huaHuiPhotosCheck(EbPhotosStop ebPhotosStop, String apiParams, File file, String licensePlate) {
        if(StrUtil.isBlank(apiParams)) {
            ebPhotosStop.setCheckStatus(PhotosStopCheckResultEnum.ERROR.getCode());
            throw new BadResponseException("缺少方案参数配置");
        };

        // 请求头
        Map<String, String> headers = new HashMap<>();
        try {
            this.analysisApiParams(headers, apiParams);
        }catch (Exception ex) {
            ebPhotosStop.setCheckStatus(PhotosStopCheckResultEnum.ERROR.getCode());
            log.error("方案参数解析异常:{}", ex.getMessage());
            throw new BadResponseException("方案参数解析异常");
        }
        headers.put("Content-Type", "multipart/form-data");
        try{
            Map<String, Object> formData = new HashMap<>();
            formData.put("file", file);
            formData.put("plateCode", licensePlate);
            HttpResponse response = HttpUtil.createPost(HUA_HUI_PORT_URL).form(formData).headerMap(headers, false).execute();
            if(response.getStatus() != 200) {
                ebPhotosStop.setCheckStatus(PhotosStopCheckResultEnum.ERROR.getCode());
                throw new BadResponseException("华惠接口调用异常");
            }
            String responseBody = response.body();
            ebPhotosStop.setCheckResult(responseBody);
            if(StrUtil.isBlank(responseBody)) {
                ebPhotosStop.setCheckStatus(PhotosStopCheckResultEnum.ERROR.getCode());
                throw new BadResponseException("未获取到响应信息");
            }
            Map<String, String> responseMap = JSONObject.parseObject(responseBody, Map.class);
            Integer responseCode = MapUtil.getInt(responseMap, "code", 1000);
            PhotosStopResponseCodeEnum responseCodeEnum = PhotosStopResponseCodeEnum.getByHuaHuiCode(responseCode);
            if(responseCodeEnum.getHuaHuiCode().equals(PhotosStopResponseCodeEnum.SUCCESS.getHuaHuiCode())) {
                ebPhotosStop.setCheckStatus(PhotosStopCheckResultEnum.PASS.getCode());
            } else {
                ebPhotosStop.setNoPassReason(responseCodeEnum.getHuaHuiCode());
                ebPhotosStop.setNoPassRemark(responseCodeEnum.getRemark());
                ebPhotosStop.setCheckStatus(PhotosStopCheckResultEnum.FAIL.getCode());
                throw new BadResponseException(responseCodeEnum.getName());
            }
        }catch (Exception ex) {
            throw new BadResponseException(ex.getMessage());
        }
    }

    private void analysisApiParams(Map<String, String> headers, String apiParams) {
        for(String params : Arrays.asList(apiParams.split("&"))) {
            headers.put(params.split("=")[0], params.split("=")[1]);
        }
    }
}
